import Vue from 'vue'
import VueRouter from 'vue-router'
// import Layout from '@/views/Layout'
Vue.use(VueRouter)

const routes = [
  {
    path: '/Login',
    name: 'Login',
    // 路由懒加载
    component: () => import('../views/Login/index.vue')
  },
  {
    path: '/reg',
    name: 'register',
    // 路由懒加载
    component: () => import('../views/reg/index.vue')
  },
  {
    path: '/',
    component: () => import('../views/layout/index.vue'),
    children: [
      {
        path: '',
        component: () => import('../views/Home/index.vue')
      },
      {
        path: 'artcategory',
        component: () => import('../views/art/artcategory.vue')
      },
      {
        path: 'artlist',
        component: () => import('../views/art/artlist.vue')
      },
      {
        path: '/userinfo',
        component: () => import('../views/user/userinfo.vue')
      },
      {
        path: '/changeavatar',
        component: () => import('../views/user/changeavatar.vue')
      },
      {
        path: '/resetpwd',
        component: () => import('../views/user/resetpwd.vue')
      }
    ]
  }
]
//  首页入口
const router = new VueRouter({
  routes
})
// 路由前置导航守卫
// 白名单列表 不需要拦截
const whiteList = ['/login', '/reg']
router.beforeEach((to, from, next) => {
  // 先判断是否访问的是有权限的页面
  if (whiteList.includes(to.path)) {
    // 用户访问的是登录或者注册页面 不需要拦截 直接放行
    next()
  } else {
    // 需要访问权限，判断是否有token
    // 如果有token直接放行
    if (localStorage.getItem('token')) {
      next()
    } else {
      // 如果没有token 强制跳转登录页面
      next({ path: '/login' })
    }
  }
})
export default router
